package com.app.utils.redis;

import com.app.utils.base.j2se.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Description
 * Created by 王伟
 * Date create on 17:24 2019/11/13.
 */
@Configuration
public class Redis{
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public static Key key;
    public static Str str;
    public static List list;
    public static Hash hash;

    @PostConstruct
    private void init(){
        key = new Key(stringRedisTemplate);
        str = new Str(stringRedisTemplate);
        list = new List(stringRedisTemplate);
        hash = new Hash(stringRedisTemplate);
    }


    /**
     * Description 用于操作Redis 的 Key
     * Created by 王伟
     * Date create on 17:24 2019/12/8.
     */
    public class Key{
        private StringRedisTemplate redis;
        Key(StringRedisTemplate redis){
            this.redis = redis;
        }

        public Long getExpire(String k){
            return redis.getExpire(k);
        }

        public void del(String k){redis.delete(k);}
    }

    /**
     * Description 用于操作Redis 的 Str
     * Created by 王伟
     * Date create on 17:24 2019/12/8.
     */
    public class Str{
        private StringRedisTemplate redis;
        Str(StringRedisTemplate redis){
            this.redis = redis;
        }

        public void set(String k,Object v,int timeOut){
            redis.opsForValue().set(k, Convert.objToStr(v), timeOut, TimeUnit.SECONDS);
        }

        public String get(String k){
            return redis.opsForValue().get(k);
        }

        public String getAndSet(String k,Object v, int timeOut){
            redis.expire(k, timeOut, TimeUnit.SECONDS);
            return redis.opsForValue().getAndSet(k,Convert.objToStr(v));
        }

        public String getAndExpire(String k, int timeOut){
            redis.expire(k, timeOut, TimeUnit.SECONDS);
            return redis.opsForValue().get(k);
        }

    }

    /**
     * Description 用于操作Redis 的 List
     * Created by 王伟
     * Date create on 17:24 2019/12/8.
     */
    public class List{
        private StringRedisTemplate redis;
        List(StringRedisTemplate redis){
            this.redis = redis;
        }

        public String rightPop(String k,int timeOut){
            if(timeOut==0)return redis.opsForList().rightPop(k);
            return redis.opsForList().rightPop(k,timeOut,TimeUnit.SECONDS);
        }

        public Long lpush(String k,String v){
            return redis.opsForList().leftPush(k, v);
        }
    }

    /**
     * Description 用于操作Redis 的 Hash
     * Created by 王伟
     * Date create on 17:24 2019/12/8.
     */
    public class Hash{
        private StringRedisTemplate redis;
        Hash(StringRedisTemplate redis){
            this.redis = redis;
        }

        public void hmset(String k, final Map<String, String> hash,int timeOut){
            redis.opsForHash().putAll(k, hash);
            redis.expire(k, timeOut, TimeUnit.SECONDS);
        }

        public Map hgetall(String k){
            return redis.opsForHash().entries(k);
        }

        public Long hincrBy(String k,String hash, long v,int timeOut){
            Long num = redis.opsForHash().increment(k,hash,v);
            redis.expire(k, timeOut, TimeUnit.SECONDS);
            return num;
        }
    }
}
